﻿using Cysharp.Threading.Tasks;
using System;
using UnityEngine;
using UniRx;
using UniRx.Toolkit;

public class FSMStateStandSkill : FSMStateBase
{
    protected override string getName => "standSkill";
    protected override string InitClipName() => null;//每个技能动作不同，在技能中配置

    private float speed => UnitDefine.BASE_SPEED * fsm.entity.attribute.speed;

    public override bool isLoop => false;


    private SkillBase m_ExecutingSkill;

    protected async override void OnEnter()
    {
        var skillId = fsm.parameters.GetParam<int>("standSkill");
        m_ExecutingSkill = fsm.entity.skillManager.skills.Find(s =>
            s.config.id == skillId &&
            s.config.effectiveStates.HasFlag(SkillEffectiveState.Standing) &&
            s.config.skillType == SkillType.Active);
        if (m_ExecutingSkill == null)
        {
            isEnd = true;
            return;
        }
        await m_ExecutingSkill.Execute();
        m_ExecutingSkill = null;
        isEnd = true;
    }

    protected override void OnExecute()
    {
        StopMove();
    }

    private void StopMove()
    {
        var easingAxis = Mathf.MoveTowards(fsm.entity.physics.velocity.x / speed, 0, Time.deltaTime / UnitDefine.BASE_ACCE_TIME);
        fsm.entity.physics.velocity = new Vector2(easingAxis * speed, fsm.entity.physics.velocity.y);
    }

    protected override void OnExit()
    {
        if (m_ExecutingSkill != null)
        {
            m_ExecutingSkill.Shutdown();
        }
    }
}